<?php

/*
 * - Okek CMS
 * - create by Jg and Tito
 * - Okek Projects 2012
 * - CMS developed for free use
 * - More info http://habbo-dev.com
 */

class Tpl {
    
    public $buffer = ''; 
    public $params = [];
    public $vars = null;
    public $error = false;
    public $head = [];
    public $lang_selection = [];
    public $l;
    
    public function Init() {
        
        global $cms_config, $langs, $captcha_public_key, $config;
        
        // set Lang
        
        $lang = ($config[1]['lang'] ? $config[1]['lang'] : USER_LANG);
        
        $this->l = $lang;
        
        $langs->Init($lang);
        
        
        
        $this->SetParam('www', WWW);
        $this->SetParam('https', HTTPS);
        $this->SetParam('sll', HTTPS);
        $this->SetParam('site_name', $cms_config['hotel_name']);
        $this->SetParam('web-gallery', WEB_GALLERY);
        $this->SetParam('web_version', $cms_config['web_build_version']);
        $this->SetParam('captcha_key', $captcha_public_key);
    }
    public function Proccess($file) {
       
        global $error, $cms_config, $tpl, $users, $vars;
        if(!$error) {
            if(!file_exists($file)) {
                $file = str_replace(NEC . 'templates' . DS, "", $file);
                $e = ["Tpl", "%error_tplLoad% <b>$file</b>"];
                LaunchError($e);
                return false;
            }
            
            ob_start();
            include $file;
            $html = ob_get_contents();
            ob_clean();
            
            $this->buffer .= $html;
            return true;
        }
        else
            return false;
    }
    public function AddTemplate($tplname) {
        
        $file = NEC . 'templates' . DS . $tplname . '.tpl';
        return $this->Proccess($file);
    }
    public function GenericSet($set) {
        switch ($set)
        {
            case 'frontpage':
                $this->SetStyle('frontpage.css');
                $this->SetJs('libs2.js');
                $this->SetJs('landing.js');
                
                $code = '<style type="text/css">
            body {
                 background-color: #000000;
                
            }
            #footer .footer-links   { color: #666666; }
            #footer .footer-links a { color: #ffffff; }
            #footer .copyright      { color: #666666; }
            #footer #compact-tags-container span, #footer #compact-tags-container a { color: #333333; }
        </style>';
                
                $this->SetCode($code, 2);
                break;
            
            case 'quickregister':
                
                $this->GenericSet();
                $this->SetStyle('quickregister.css', 2);
                $this->SetJs('quickregister.js', 2);
                
                if($this->vars['quickregister_step'] == 'captcha') {
                    $this->SetJs('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js', 2, false);
                }
                break;
                
            default:
                $this->SetStyle('common.css');
                $this->SetJs('libs2.js');
                $this->SetJs('visual.js');
                $this->SetJs('libs.js');
                $this->SetJs('common.js');
                break;
        }
    }
    public function SetStyle($file, $set = 1, $auto = true) {
        $file = ($auto ? WEB_GALLERY . '/static/styles/' . $file : $file);
        $this->head[$set] .= "<link rel='stylesheet' href='$file' type='text/css' />";
    }
    public function SetJs($file, $set = 1, $auto = true) {
        $file = ($auto ? WEB_GALLERY . '/static/js/' . $file : $file);
        $this->head[$set] .= "<script src='$file' type='text/javascript'></script>";
    }
    public function SetCode($code, $set = 1) {
        $this->head[$set] .= $code;
    }
    public function SetParam($param, $value) {
        if(empty($value)) {
            $value='';
        }
        $this->params[$param] = $value;
    }
    public function SetVars($param, $value) {
        if(empty($value)) {
            $value=null;
        }
        $this->vars[$param] = $value;
    }
    public function FilterParams($html) {
        
        global $langs;
        
        foreach($this->params as $var => $value) {
            $html = str_replace('[' . strtolower($var) . ']', $value, $html);
        }
        
        return $html;
    }
    public function SetLang($sections) { 
        global $langs;
        
        array_push($sections, 'global');
        array_push($sections, 'meta');
        array_push($sections, 'javascript');
        array_push($sections, 'cookies');
        
        if(!LOGGED_IN)
            array_push($sections, 'forgot');
        
        $this->lang_selection = $sections;
        
        
    }
    public function getSite() {
        
        
        global $langs;
        
        $hlang = $langs->PrepareLive();
        $hlang = json_encode($hlang);
        
        $this->SetParam('hlang', $hlang);
        $html = $this->GetBuffer();
        $html = $langs->SetParams($html, $this->lang_selection);
        echo $html;
    }
    public function GetBuffer() {
        return $this->FilterParams($this->buffer);
    }
    public function setError($e) {
        
        $tpl = new Tpl();
        $tpl->Init();
        $tpl->SetLang(['error']);
        $tpl->SetParam('error_type', $e[0]);
        $tpl->SetParam('error_code', $e[1]);
        $tpl->SetParam('error', $e[2]);
        $tpl->AddTemplate('errorPage');
        $tpl->getSite();
        exit;
    }
}
?>
